package com.example.examplexml;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;




import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);
        
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
      
       
        String xml = XMLfunctions.getXML("http://210.211.98.84/webicm/ts/ProvinceLottery.xml");
        
        Document doc = XMLfunctions.XMLfromString(xml);
                
        NodeList nodes = doc.getElementsByTagName("item");
        
        
		for (int i = 0; i < nodes.getLength(); i++) {							
			HashMap<String, String> map = new HashMap<String, String>();	
			Node itemNode = nodes.item(i);
			
			Element definitionElement = (Element) itemNode;
			
			NodeList wordDefinitionElements = (definitionElement).getElementsByTagName("ProvinceID");
			
			for(int j=0; j <wordDefinitionElements.getLength(); j++){
				
				Element wordDefinitionElement = (Element) wordDefinitionElements.item(j);
				
				
				map.put("id", XMLfunctions.getValue(wordDefinitionElement, "ProvinceID"));
				
				
			}
			NodeList wordDefinitionElements2 = (definitionElement).getElementsByTagName("ProvinceName");
			for(int j=0; j <wordDefinitionElements2.getLength(); j++){
				
				Element wordDefinitionElement = (Element) wordDefinitionElements2.item(j);
				
				
				map.put("ProvinceName","Province: "+ XMLfunctions.getValue(wordDefinitionElement, "ProvinceName"));
				
				
			}
			
			mylist.add(map);	
		}		
       
        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.activity_main, 
                        new String[] { "id", "ProvinceName" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle });
        
        setListAdapter(adapter);
        
        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);	
        lv.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        		
        		@SuppressWarnings("unchecked")
				HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);	        		
        		//Toast.makeText(MainActivity.this, "ID " + o.get("id")+" "+  o.get("ProvinceName")+ " was clicked.", Toast.LENGTH_LONG).show(); 
        		Bundle sendbundle = new Bundle();
        		sendbundle.putString("id", o.get("id"));
        		Intent i = new Intent(MainActivity.this,list2.class);
        		i.putExtras(sendbundle);
        		startActivity(i);
        		
			}
		});
    }
}